<!-- pages/new/_id.vue -->
<template>
  <div class="article-detail container">
    <div class="content-wrapper">
      <div class="back-link">
        <nuxt-link to="/news">« 返回{{typeName}}列表</nuxt-link>
      </div>
      
      <div v-if="article">
        <h1>{{ article.title }}</h1>
        
        <div class="meta">
          <span><i class="fas fa-user"></i> {{ article.author }}</span>
          <span><i class="far fa-calendar-alt"></i> {{ article.date }}</span>
          <span><i class="far fa-eye"></i> {{ article.views }} 次阅读</span>
        </div>
        
        <div class="tags">
          <span v-for="(tag, index) in article.tags" :key="index" class="tag">
            {{ tag }}
          </span>
        </div>
        
        <div class="summary">
          {{ article.summary }}
        </div>
        
        <div class="content" v-html="article.content"></div>
      </div>
      
      <div v-else class="loading">
        正在加载文章...
      </div>
    </div>
  </div>
</template>

<script>
export default {
  async asyncData({ $axios, params, error }) {
    console.log(params);
    const { type, id } = params;
    try {
      const { data } = await $axios.get(`/api/${type}/${id}`);
      return { article: data };
    } catch (err) {
      error({ statusCode: 404, message: '文章不存在' });
      return { article: null };
    }
  },
  computed: {
    typeName() {
      const typeNames = {
        'top': '话题',
        'news': '新闻',
        'show': '展示',
        'jobs': '工作',
      }
      return typeNames[this.$route.params.type] || '文章';
    }
  },
  head() {
    return {
      title: this.article ? `${this.article.title} - Nuxt新闻` : 'Nuxt新闻',
      meta: [
        {
          hid: 'description',
          name: 'description',
          content: this.article ? this.article.summary : '文章详情页'
        }
      ]
    };
  }
};
</script>

<style>
.article-detail {
  padding: 20px 0;
}

.content-wrapper {
  max-width: 800px;
  margin: 0 auto;
  background-color: #fff;
  padding: 30px;
  border-radius: 5px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.back-link {
  margin-bottom: 20px;
}

.back-link a {
  color: #3498db;
  text-decoration: none;
  font-size: 16px;
}

.back-link a:hover {
  text-decoration: underline;
}

h1 {
  font-size: 28px;
  color: #2c3e50;
  margin-bottom: 15px;
}

.meta {
  color: #7f8c8d;
  margin-bottom: 20px;
  font-size: 0.9em;
}

.meta span {
  margin-right: 20px;
}

.tags {
  margin-bottom: 25px;
}

.tag {
  background-color: #f1f1f1;
  color: #666;
  padding: 5px 10px;
  border-radius: 15px;
  font-size: 0.85em;
  margin-right: 8px;
  display: inline-block;
}

.summary {
  font-size: 18px;
  color: #34495e;
  margin-bottom: 30px;
  padding: 15px;
  background-color: #f9f9f9;
  border-left: 4px solid #3498db;
}

.content {
  line-height: 1.8;
  color: #333;
}

.content p {
  margin-bottom: 20px;
}

.content pre {
  background-color: #f8f8f8;
  padding: 15px;
  border-radius: 5px;
  overflow: auto;
  margin-bottom: 20px;
}

.loading {
  text-align: center;
  padding: 50px;
  color: #7f8c8d;
}
</style>